package com.example.musicplayer.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.musicplayer.R;
import com.example.musicplayer.adapter.MusicAdapter;
import com.example.musicplayer.adapter.MusicListAdapter;
import com.example.musicplayer.entity.Music;
import com.example.musicplayer.entity.MusicList;
import com.example.musicplayer.service.MusicService;
import com.example.musicplayer.util.DbUtil;

import java.util.ArrayList;
import java.util.List;

public class MusicListActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar mToolBar;
    public static TextView mTitle; // 标题栏
    private ImageView mBack; // 返回上一级
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private MusicAdapter mMusicAdapter;
    private MusicService.MusicBinder mMusicService = MusicService.getMusicService();
    private List<Music> mMusics = new ArrayList<>();
    private List<MusicList> mMusicLists = new ArrayList<>();
    private MusicListAdapter mMusicListAdapter; // 歌单列表适配器
    private DbUtil mDbUtil = new DbUtil();

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_list);
        initView();
        mRecyclerView.setItemViewCacheSize(200);
    }

    // 当删除歌单中的歌曲后，回到歌单列表界面，可以刷新新的歌曲数量
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String key = intent.getStringExtra("title");
        String request_code = intent.getStringExtra("request_code");
        String musicId = intent.getStringExtra("musicId");
        mTitle.setText(key);
        Log.i("=================", "设置了标题栏" + key);
        if ("我喜欢".equals(key)) {
            mMusics = mDbUtil.getMusicInMusicList(this, "我喜欢");
            mLinearLayoutManager = new LinearLayoutManager(this);
            mMusicAdapter = new MusicAdapter(getApplicationContext(), mMusics);
            mRecyclerView.setLayoutManager(mLinearLayoutManager);
            mRecyclerView.setAdapter(mMusicAdapter);
        } else if ("本地音乐".equals(key)) {
            mMusics = mMusicService.getLocalMusicList();
            mLinearLayoutManager = new LinearLayoutManager(this);
            mMusicAdapter = new MusicAdapter(getApplicationContext(), mMusics);
            mRecyclerView.setLayoutManager(mLinearLayoutManager);
            mRecyclerView.setAdapter(mMusicAdapter);
        } else if ("歌单".equals(key)) {
            mMusicLists = mDbUtil.getMusicList(this);
            mLinearLayoutManager = new LinearLayoutManager(this);
            mMusicListAdapter = new MusicListAdapter(this, mMusicLists, R.layout.recyclerview_music_list_item_vertical, request_code, musicId);
            mRecyclerView.setLayoutManager(mLinearLayoutManager);
            mRecyclerView.setAdapter(mMusicListAdapter);
        } else if ("已购".equals(key)) {
            Log.i("============", "您选择了已购歌单");
        } else {    // 普通歌单
            mMusics = mDbUtil.getMusicInMusicList(this, key);
            mLinearLayoutManager = new LinearLayoutManager(this);
            mMusicAdapter = new MusicAdapter(getApplicationContext(), mMusics);
            mRecyclerView.setLayoutManager(mLinearLayoutManager);
            mRecyclerView.setAdapter(mMusicAdapter);
        }
    }

    private void initView() {
        mToolBar = findViewById(R.id.music_list_page_toolbar);
        mTitle = findViewById(R.id.activity_music_list_title);
        mBack = findViewById(R.id.back);
        mRecyclerView = findViewById(R.id.activity_music_list_recycler_view);
        mBack.setOnClickListener(this);
    }


    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }
}